#include"w5500_set.h"
#include"spi_tx_rx.h"
#include "regs.h"
#include "w5500.h"
#include"socket.h"



uint8_t mac1[6]={0x00,0x08,0xdc,0x11,0x11,0x11};
uint8_t local_ip1[4] = {192,168,137,220};                        // 定义w5500默认IP地址
uint8_t subnet1[4] = {255,255,255,0};                          // 定义w5500默认子网掩码
uint8_t gateway1[4] = {192,168,137,1};                           // 定义w5500默认网关
uint8_t dns_server1[4] = {192,168,137,1};                    // 定义w5500默认DNS

uint8_t mac2[6]={0x00,0x08,0xdc,0x11,0x11,0x12};
uint8_t local_ip2[4] = {192,168,137,221};                        // 定义w5500默认IP地址
uint8_t subnet2[4] = {255,255,255,0};                          // 定义w5500默认子网掩码
uint8_t gateway2[4] = {192,168,137,1};                           // 定义w5500默认网关
uint8_t dns_server2[4] = {192,168,137,1};                    // 定义w5500默认DNS

uint16_t local_port = 5000;



/*定义远端IP信息,w5500作客户端时连接的服务器ip和端口*/
uint8_t  remote_ip[4]={192,168,137,1};											/*远端IP地址*/
uint16_t remote_port=6000;																/*远端端口号*/


uint8_t buff[2048];				                              	         /*定义一个2KB的缓存*/

/**
*@brief  读取PHY寄存器数据
*@param  无
*@return PHYSR寄存器数值
*/

uint8_t getPHYStatus( int fd)
{
  return IINCHIP_READ(PHYCFGR,fd);
}
void	set_w5500_mac(int fd,int num)
{
	if(num == 0)
	wiz_write_buf(fd,SHAR0,mac1, 6); 
	else
	wiz_write_buf(fd,SHAR0,mac2, 6); 
}
void set_w5500_ip(int fd,int num)
{
	if(num == 0)
	{
		/*将IP配置信息写入W5500相应寄存器*/	
		wiz_write_buf(fd,SUBR0, subnet1, 4);
		wiz_write_buf(fd,GAR0, gateway1, 4);
		wiz_write_buf(fd,SIPR0, local_ip1, 4);
	}
	else
	{
		/*将IP配置信息写入W5500相应寄存器*/	
		wiz_write_buf(fd,SUBR0, subnet2, 4);
		wiz_write_buf(fd,GAR0, gateway2, 4);
		wiz_write_buf(fd,SIPR0, local_ip2, 4);
	}
    uint8_t rx[10] = {0};
    wiz_read_buf(fd,SIPR0, rx, 4);			
	printf(" W5500 IP地址   : %d.%d.%d.%d\r\n", rx[0],rx[1],rx[2],rx[3]);
     wiz_read_buf(fd,SUBR0, rx, 4);	
	printf(" W5500 子网掩码 : %d.%d.%d.%d\r\n",rx[0],rx[1],rx[2],rx[3]);
    wiz_read_buf(fd,GAR0, rx, 4);
	printf(" W5500 网关     : %d.%d.%d.%d\r\n", rx[0],rx[1],rx[2],rx[3]);
}

//检查连没连网线
int PHY_check(int fd)
{
  uint8_t PHY_connect=0;
  PHY_connect=0x01&getPHYStatus(fd);
  if(PHY_connect==0)
  {
    printf(" \r\n 请检查网线是否连接?\r\n");
    PHY_connect = 0;
    do
    {
      close_w5500(PHY_connect++,fd);
    }while(PHY_connect < 4);
    PHY_connect = 0;
    // while(PHY_connect == 0) 
	// 	{
    //   PHY_connect=0x01&getPHYStatus(fd);
    //   printf(" .");
    //   sleep(1);
    // }
	return 0;
    printf(" \r\n");
  }
  return 1;
}


/**
*@brief		TCP Server回环演示函数。
*@param		无
*@return	无
*/
void do_tcp_server(int fd,uint8_t SOCK)
{	

	uint16_t len=0;  
  uint8_t flag = getSn_SR(SOCK,fd);
  // printf("flag = %x\n",flag);
	switch(flag)											            	/*获取socket的状态*/
	{
		case SOCK_CLOSED:													                  /*socket处于关闭状态*/
			socket_w5500(SOCK ,Sn_MR_TCP,local_port,Sn_MR_ND,fd);	        /*打开socket*/
		  break;     
    
		case SOCK_INIT:														                  /*socket已初始化状态*/
			listen_w5500(SOCK,fd);												                /*socket建立监听*/
		  break;				                  
		case SOCK_ESTABLISHED:												              /*socket处于连接建立状态*/
		
			if(getSn_IR(SOCK,fd) & Sn_IR_CON)
			{
				setSn_IR(SOCK, Sn_IR_CON,fd);								          /*清除接收中断标志位*/
			}
			len=getSn_RX_RSR(SOCK,fd);									            /*定义len为已接收数据的长度*/
			if(len>0)
			{
				recv_w5500(SOCK,buff,len,fd);								              	/*接收来自Client的数据*/
				buff[len]=0x00; 											                  /*添加字符串结束符*/
				printf("%s\r\n",buff);
				send_w5500(SOCK,buff,len,fd);									              /*向Client发送数据*/
		  }
		  break;
		
		case SOCK_CLOSE_WAIT:												                /*socket处于等待关闭状态*/
			close_w5500(SOCK,fd);
		  break;
	}
}


void do_tcp_client(int fd,uint8_t SOCK)
{	
   uint16_t len=0;	

	switch(getSn_SR(SOCK,fd))								  				         /*获取socket的状态*/
	{
		case SOCK_CLOSED:											        		         /*socket处于关闭状态*/
			socket_w5500(SOCK,Sn_MR_TCP,local_port++,Sn_MR_ND,fd);
//      printf("SOCK_CLOSED\n");
		  break;
		
		case SOCK_INIT:													        	         /*socket处于初始化状态*/
			connect_w5500(SOCK,remote_ip,remote_port,fd);                /*socket连接服务器*/ 
//      printf("SOCK_INIT\n");
		  break;
		
		case SOCK_ESTABLISHED: 												             /*socket处于连接建立状态*/
			if(getSn_IR(SOCK,fd) & Sn_IR_CON)
			{
				setSn_IR(SOCK, Sn_IR_CON,fd); 							         /*清除接收中断标志位*/
			}
		
			len=getSn_RX_RSR(SOCK,fd); 								  	         /*定义len为已接收数据的长度*/
			if(len>0)
			{
				recv_w5500(SOCK,buff,len,fd); 							   		         /*接收来自Server的数据*/
				buff[len]=0x00;  											                 /*添加字符串结束符*/
				printf("%s\r\n",buff);
				send_w5500(SOCK,buff,len,fd);								     	         /*向Server发送数据*/
			}	
//      printf("SOCK_ESTABLISHED\n");      
		  break;
			
		case SOCK_CLOSE_WAIT: 											    	         /*socket处于等待关闭状态*/
			close_w5500(SOCK,fd);
//      printf("SOCK_CLOSE_WAIT\n");
		  break;
	}
  
}

void reset_w5500()
{
    NETWORK_2_RST_0;
    sleep(2);
    NETWORK_2_RST_1;

    NETWORK_3_RST_0;
    sleep(2);
    NETWORK_3_RST_1;
}

void  NETWORK_EXport()
{
    /*删除之前的节点*/
    // system("echo 92 > /sys/class/gpio/unexport");
    // system("echo 93 > /sys/class/gpio/unexport");
    // system("echo 94 > /sys/class/gpio/unexport");
    // system("echo 95 > /sys/class/gpio/unexport");

    /*导出GPIO*/
    system("echo 92 > /sys/class/gpio/export");
    system("echo 93 > /sys/class/gpio/export");
    system("echo 94 > /sys/class/gpio/export");
    system("echo 95 > /sys/class/gpio/export");

    /*配置为输出*/
    system(" echo out > /sys/class/gpio/gpio95/direction");
    system(" echo out > /sys/class/gpio/gpio93/direction");
    /*配置为输入*/
    system(" echo out > /sys/class/gpio/gpio92/direction");
    system(" echo out > /sys/class/gpio/gpio94/direction");
}